import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_first_station/article/api/article_api.dart';
import 'package:flutter_first_station/article/views/article_detail.dart';

import '../model/article_model.dart';

class ArticleContent extends StatefulWidget {
  const ArticleContent({super.key});

  @override
  State<ArticleContent> createState() => _ArticleContentState();
}

class _ArticleContentState extends State<ArticleContent> {
  List<Article> _articles = [];

  ArticleApi api = ArticleApi();

  bool _loading = false;

  @override
  void initState() {
    super.initState();
    _loadData();
  }

  void _loadData() async {
    _loading = true;
    setState(() {});
    _articles = await api.loadArticles(0);
    _loading = false;
    setState(() {});
  }

  // 刷新
  void _onRefresh() async {
    _articles = await api.loadArticles(0);
    setState(() {});
  }

  // 加载更多
  void _onLoad() async {
    int nextPage = _articles.length ~/ 20 + 1;
    List<Article> newArticles = await api.loadArticles(nextPage);
    _articles = _articles + newArticles;
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    if (_loading) {
      return const Center(
        child: Wrap(
          spacing: 10,
          direction: Axis.vertical,
          crossAxisAlignment: WrapCrossAlignment.center,
          children: [
            CupertinoActivityIndicator(),
            Text(
              "数据加载中，请稍后...",
              style: TextStyle(color: Colors.grey),
            )
          ],
        ),
      );
    }
    return EasyRefresh(
        header: const ClassicHeader(
            dragText: '下拉加载',
            armedText: '释放刷新',
            readyText: '开始加载',
            processedText: '刷新成功',
            processingText: '正在加载'),
        onRefresh: _onRefresh,
        onLoad: _onLoad,
        child: ListView.builder(
            itemExtent: 80,
            itemCount: _articles.length,
            itemBuilder: _buildItemByIndex));
  }

  Widget _buildItemByIndex(BuildContext context, int index) {
    Article article = _articles[index];
    return GestureDetector(
      onTap: () => _jumpToPage(article),
      child: ListTile(
        title: Text(
          article.title,
          style: const TextStyle(
              color: Colors.black,
              fontSize: 14,
              fontWeight: FontWeight.bold,
              overflow: TextOverflow.ellipsis),
        ),
        subtitle: Text(
          article.url,
          style: const TextStyle(
            color: Colors.grey,
            fontSize: 12,
            overflow: TextOverflow.ellipsis,
          ),
        ),
        trailing: Text(
          article.time,
          style: const TextStyle(
            color: Colors.grey,
            fontSize: 12,
          ),
        ),
      ),
    );
  }

  void _jumpToPage(Article article) {
    Navigator.push(context,
        MaterialPageRoute(builder: (_) => ArticleDetailPage(article: article)));
  }
}
